package com.maotai.app.thread;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.http.client.config.RequestConfig;

import Decoder.BASE64Encoder;
import cn.hellotrue.HelloTrue;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.maotai.app.bean.MaotaiAppCookie;
import com.maotai.app.bean.MaotaiAppString;
import com.maotai.app.service.MaotaiAppService;
import com.maotai.app.util.MaotaiAppHttpClientUtil;
import com.maotai.llzhou.util.IPFactory;

public class MaoTaiAppRegisterThread extends Thread {

	private MaotaiAppService maotaiAppService;
	private String token;
	

	public MaoTaiAppRegisterThread(MaotaiAppService maotaiAppService,
			String token) {
		this.maotaiAppService = maotaiAppService;
		this.token = token;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public MaotaiAppService getMaotaiAppService() {
		return maotaiAppService;
	}

	public void setMaotaiAppService(MaotaiAppService maotaiAppService) {
		this.maotaiAppService = maotaiAppService;
	}

	@Override
	public void run() {
		HelloTrue ht = new HelloTrue();
		String telphone = null;
		try {
			MaotaiAppCookie mwc = new MaotaiAppCookie();
			BASE64Encoder encode = new BASE64Encoder();
			MaotaiAppHttpClientUtil hcu = MaotaiAppHttpClientUtil.getInstance();
			JSONArray jArray = IPFactory.getIpAndPort();
			JSONObject jj = (JSONObject) jArray.get(0);
			String ipAndPort = jj.getString("ip") + ":"
					+ jj.getString("port");
			//现货区号码
			
			telphone = ht.getPhone(token);
			if(telphone.contains("最大号码")){
				return;
			}
			//查询库里是不是有了
			Map<String, String> phoneMap = new HashMap<String, String>();
			phoneMap.put("telphone", telphone);
			List<Map<String,Object>> list = maotaiAppService.getPhone(phoneMap);
			if(list!=null && list.size()>0){
				System.out.println(telphone+"::库里有该号码");
				return;
			}
			//发送验证码到手机
			String send_url = MaotaiAppString.SEND_MOBILE_URL.replace("xxx", telphone);
			
			RequestConfig config = IPFactory.getKdRequestConfig(ipAndPort);
			mwc = hcu.sendHttpGetProxy(send_url, mwc, config, null);
			//{"data":"发送成功","resultCode":0,"resultMsg":"success"}
			/**
				 * userName:13372007847
				password:cTE5ODYwNjE4
				mobile:13372007847
				verifyCode:123456
				confimpwd:cTE5ODYwNjE4
				appCode:newrretail
				channel:B2C
				 */
			//接收验证码
			//TODO
			String pwd = encode.encode(("a"+telphone).getBytes());
			pwd = pwd.replace("=", "");
			System.out.println(telphone+"的密码为:"+pwd);
			String message = ht.getMessage(token, telphone);
			System.out.println("获取到的验证码信息为：" + message);
			if("超时".equals(message)){
				try {
					throw new Exception("未获取到验证码");
				} catch (Exception e) {
				}
			}
			String regEx="[^0-9]";   
			Pattern p = Pattern.compile(regEx);   
			String telCode = p.matcher(message).replaceAll("").trim().substring(0,6);
			System.out.println("截取后的验证码信息为：" + telCode);
			//channel=O2O&mobile=15895887847&password=cTEyMzQ1Njc4&verifyCode=123456
				Map<String, String> registerMap = new HashMap<String, String>();
				registerMap.put("userName", telphone);
				registerMap.put("password", pwd);
				registerMap.put("mobile", telphone);
				registerMap.put("verifyCode", telCode);
				registerMap.put("channel", "O2O");
				mwc = hcu.sendHttpPostProxy(MaotaiAppString.REGISTER_URL, mwc, registerMap ,config,null);
//				mwc = hcu.sendHttpPostSendSms(MaotaiString.SENDSCM_URL, mwc, registerMap, "https://www.emaotai.cn/Register.aspx");
				
				//{"data":null,"resultCode":-1,"resultMsg":"验证码错误"}
				if(mwc.getContent().contains("success")){
					System.out.println(telphone+" 手机号注册成功");
					ht.addBlacklist(token, telphone);
					Map<String,String> map = new HashMap<String,String>();
					map.put("code", telphone);
					map.put("pwd", "a"+telphone);
					map.put("phone", telphone);
					maotaiAppService.insertMaotaiApp(map);
				}
		} catch (Exception e1) {
			e1.printStackTrace();
			ht.addBlacklist(token, telphone);
		}
	}
}
